/*
 * Copyright (C) 2009 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.bingluo.livewallpaper.hunterxhunter;

import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.view.Window;
import android.widget.Toast;
import cn.waps.UpdatePointsNotifier;

import com.allcode.ad2.WPu;

public class MainWallpaperServiceSettings extends PreferenceActivity
    implements SharedPreferences.OnSharedPreferenceChangeListener,OnPreferenceClickListener,UpdatePointsNotifier {
//	HomeTabActivity tabActivity = null;
	SharedPreferences shp;
	
	CheckBoxPreference open_setting_box = null;//打开设置
	CheckBoxPreference on_off_select_bg_settings = null;//固定背景
	CheckBoxPreference edge_auto_change_bg = null;//边界换图
	Preference about_me = null;//关于我们
	Preference free_statement = null;//免费声明
	
	WPu wpu = null;
    @Override
    protected void onCreate(Bundle icicle) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(icicle);
//        tabActivity = (HomeTabActivity)getParent();
        getPreferenceManager().setSharedPreferencesName(
                MainWallpaperService.SHARED_PREFS_NAME);
        addPreferencesFromResource(R.xml.cube2_settings);
        getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(
                this);
        shp = getSharedPreferences(MainWallpaperService.SHARED_PREFS_NAME, 0);

		open_setting_box = (CheckBoxPreference)findPreference("open_off_settings");
		on_off_select_bg_settings = (CheckBoxPreference)findPreference("on_off_select_bg_settings");
		edge_auto_change_bg = (CheckBoxPreference)findPreference("edge_auto_change_bg");
		about_me = (Preference)findPreference("about_me");
		free_statement = (Preference)findPreference("free_statement");
		
		open_setting_box.setOnPreferenceClickListener(this);
		about_me.setOnPreferenceClickListener(this);
		free_statement.setOnPreferenceClickListener(this);
		
		on_off_select_bg_settings.setOnPreferenceClickListener(this);
		edge_auto_change_bg.setOnPreferenceClickListener(this);
		
		wpu = new WPu(this, this);
    }

    @Override
    protected void onResume() {
		wpu.getPoints();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(
                this);
        wpu.onDestroy();
        super.onDestroy();
    }

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
            String key) {
    }
    
	@Override
	public boolean onPreferenceClick(Preference preference) {
		// TODO Auto-generated method stub
		String key = preference.getKey();
		if (key.equals("open_off_settings")) {
			String title = "您当前的积分不够，您当前积分为"+wpu.integral+"分,需消耗积分为"+wpu.expenseIntegral+"分,下载安装并使用推荐的应用,可获取相应的积分。";
			boolean isOpenSetting = shp.getBoolean("isOpenSetting", false);
			if (!isOpenSetting) {
				open_setting_box.setChecked(false);
	    		if (!wpu.isEnoughIntegral(wpu.integral, title)) {
					return false;
				}
			}
			if (!isOpenSetting) {
				wpu.hintDialog("开启功能将扣除"+wpu.expenseIntegral+"积分,您当前积分为"+wpu.integral+"分", true, l);
			}
		}else if (key.equals("about_me")){
    		String title = getString(R.string.about_me);
    		wpu.hintDialog(title, false, l1);
		}else if (key.equals("free_statement")){
			String title = getString(R.string.free_statement);
			wpu.hintDialog(title, false, l1);
		}else if (key.equals("on_off_select_bg_settings")){
			if (on_off_select_bg_settings.isChecked()) {
				edge_auto_change_bg.setChecked(false);
			}
		}else if (key.equals("edge_auto_change_bg")){
			if (edge_auto_change_bg.isChecked()) {
	    		if (on_off_select_bg_settings.isChecked()) {
	    			edge_auto_change_bg.setChecked(false);
	    			Toast.makeText(MainWallpaperServiceSettings.this, "请先关闭固定背景", Toast.LENGTH_SHORT).show();
				}
			}
		}
		return false;
	}
	
	DialogInterface.OnClickListener l = new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int which) {
			if(which == DialogInterface.BUTTON_POSITIVE){
				open_setting_box.setChecked(true);
				wpu.spendPoints(wpu.expenseIntegral);
				SharedPreferences.Editor editor = shp.edit();
				editor.putBoolean("isOpenSetting", true);
				// 一定要提交 
				editor.commit();
			}
		}
	};
    
	DialogInterface.OnClickListener l1 = new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int which) {
		}
	};
	@Override
	public void getUpdatePoints(String arg0, int arg1) {
		wpu.integral = arg1;
	}

	@Override
	public void getUpdatePointsFailed(String arg0) {
		// TODO Auto-generated method stub
		
	}
}
